package common

import (
	"os"
	"log"
	"flag"
)

var debug = flag.Bool("d", true, "print additional debug output")

func Traceln(v ...interface{}) {
	if *debug {
		log.Println(v...)
	}
}

func SafelyDo(fname string, f func()) {
	defer func() {
		if err := recover(); err != nil {
			log.Printf("Goroutine %s terminated: %v\n", fname, err)
		}
	}()
	f()
}

func test(f func(string, ...interface{}),
err os.Error, msg string) {
	if err != nil {
		f("Fail: %s\n%v\n", msg, err)
		return
	}
	Traceln("Ok:", msg)
}

func Testp(err os.Error, msg string) {
	test(log.Panicf, err, msg)
}

func Teste(err os.Error, msg string) {
	test(log.Fatalf, err, msg)
}
